package com.ytkj.digitalConstruction.controller.gis;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ytkj.digitalConstruction.HttpResult;
import com.ytkj.digitalConstruction.httpbean.gis.GISRequestParams;
import com.ytkj.digitalConstruction.service.assets.VehicleService;
import com.ytkj.digitalConstruction.util.DateUtil;

/**
 * GIS服务请求控制器
 *
 * @author liuht
 * @phone 13823600456
 * @email laupete@qq.com
 * @date 2018年2月23日
 */
@RestController
@RequestMapping("/gis")
public class GISController {

	@Autowired
	private VehicleService vehicleServie;

	/**
	 * 车辆实时定位
	 * 
	 * @param vehicleId
	 * @return
	 */
	@GetMapping("/realtime/{id}")
	public HttpResult realtime(@PathVariable("id") int vehicleId) {
		return new HttpResult().success().obj(vehicleServie.findRealtimeById(vehicleId));
	}

	/**
	 * 车辆历史轨迹
	 * 
	 * @param vehicleId
	 * @param start
	 * @param end
	 * @return
	 */
	@PostMapping("/history")
	public HttpResult history(@RequestBody @Valid GISRequestParams params, BindingResult result) {
		if (result.hasErrors()) {
			return new HttpResult().fail().msg(result.getFieldError().getDefaultMessage());
		}
		Date startDate = DateUtil.stringToDate(params.getStart());
		Date endDate = DateUtil.stringToDate(params.getEnd());
		if (startDate == null || endDate == null) {
			return new HttpResult().fail().msg("date format error");
		}
		List<JSONObject> histories = vehicleServie.findHistoryById(params.getId(), startDate, endDate);
		return new HttpResult().success().obj(histories);
	}

}
